//
//  ViewController.m
//  UI11_Modle的使用
//
//  Created by wyx on 16/3/23.
//  Copyright © 2016年 Apon. All rights reserved.
//

#import "ViewController.h"
#import "TableViewOfContact.h"
#import "ModleOfContact.h"
#pragma mark - 声明 协议
@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, retain)UITableView *tableView;
@property (nonatomic, retain)NSDictionary *dic;
@property (nonatomic, retain)NSMutableArray *arrOFKeys;
@property (nonatomic, retain)NSMutableDictionary *dicOfNew;
@end
@implementation ViewController
#pragma mark - dealloc
-(void)dealloc{
    [_dic release];
    [_tableView release];
    [_arrOFKeys release];
    [_dicOfNew release];
    [super dealloc];
}
#pragma mark - 加载
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    //设置标题
    self.navigationItem.title = @"首页";
    self.navigationItem.rightBarButtonItem = self.editButtonItem;
    
    
    //方法调用
    [self createTableView];
    [self handleDate];
    
}
#pragma mark - Date
- (void)handleDate {
    NSString *path = [[NSBundle mainBundle] pathForResource:@"DLS160101" ofType:@"plist"];
    NSDictionary *dic = [NSDictionary dictionaryWithContentsOfFile:path];
    //新字典初始化
    self.dicOfNew = [NSMutableDictionary dictionary];
    //构建新字典里面含有 modle
    for (NSString *key in dic) {
        NSArray *arrOfContact = [dic objectForKey:key];
        
        //创建新数组
        NSMutableArray *arrOfNew = [NSMutableArray array];
        //遍历数组
        for (NSDictionary *dic in arrOfContact) {
            ModleOfContact *modle = [[ModleOfContact alloc] init];
            //modle赋值(KVC)
            [modle setValuesForKeysWithDictionary:dic];
            //modle放入新数组中
            [arrOfNew addObject:modle];
            [modle release];
        }
        //将modle 放入新字典中
        [self.dicOfNew setValue:arrOfNew forKey:key];
    }
    self.arrOFKeys = [[self.dicOfNew allKeys] sortedArrayUsingSelector:@selector(compare:)].mutableCopy;
    
    
}
#pragma mark - 创建 tableView
- (void)createTableView{
    self.tableView = [[TableViewOfContact alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];
    
    [self.view addSubview:self.tableView];
    [self.tableView release];
    //指定代理人
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.separatorColor = [UIColor redColor];
    //注册 cell
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"pool"];
    
}
#pragma mark - 分区数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.arrOFKeys.count;
}
#pragma mark - 行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSString *key = [self.arrOFKeys objectAtIndex:section];
    NSArray *arr = [self.dicOfNew objectForKey:key];
    return arr.count;
    
}
#pragma mark - cell 赋值
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"pool"];
    NSString *key = [self.arrOFKeys objectAtIndex:indexPath.section];
    NSArray *arr = [self.dicOfNew objectForKey:key];
    ModleOfContact *modle = [arr objectAtIndex:indexPath.row];
    cell.textLabel.text = modle.name;
    cell.detailTextLabel.text = modle.phoneNumber;
    
    return cell;
}
#pragma mark -  其他协议方法
/** cell 头部标题 */
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    return [self.arrOFKeys objectAtIndex:section];
}
/** 右侧索引 */
- (NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView{
    return self.arrOFKeys;
}
#pragma mark - tableView 的编辑
/** 重写 setEditing方法 */
- (void)setEditing:(BOOL)editing animated:(BOOL)animated{
    [super  setEditing:editing animated:animated];
    [self.tableView setEditing:editing animated:animated];
}
/** 那些行可以进行编辑 */
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
        return YES;
}
/** 每行编辑风格 */
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
    //第二行是插入,其他是删除.
    if (indexPath.row == 1) {
        return UITableViewCellEditingStyleInsert;
    }
    else{
        return UITableViewCellEditingStyleDelete;
    }
}
/** 提交编辑 */
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    //当点击删除
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        //更新数据
        NSString *key = [self.arrOFKeys objectAtIndex:indexPath.section];
        NSMutableArray *arr = [self.dicOfNew objectForKey:key];
        [arr removeObjectAtIndex:indexPath.row];
        //删除相应的 row
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
    }//点击插入
    else if(editingStyle == UITableViewCellEditingStyleInsert){
        //更新数据
        NSString *key = [self.arrOFKeys objectAtIndex:indexPath.section];
        NSMutableArray *arr = [self.dicOfNew objectForKey:key];
        [arr insertObject:@"lili" atIndex:indexPath.row];
        //插入相应的 row
        [tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
    }
}
#pragma mark - tableView 移动
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath{
    return YES;
}
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath{
    //更新数据
    NSString *source = [[self.arrOFKeys objectAtIndex:fromIndexPath.row]retain];
    [self.arrOFKeys removeObjectAtIndex:fromIndexPath.row];
    [self.arrOFKeys insertObject:source atIndex:toIndexPath.row];
    [source release];
}
#pragma mark - other
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
